package com.zhuiYun.project.api.ordersappraise.service.impl;

import com.zhuiYun.project.api.ordersappraise.entity.OrdersAppraiseEntity;
import com.zhuiYun.project.api.ordersappraise.mapper.OrdersAppraiseMapper;
import com.zhuiYun.project.api.ordersappraise.service.OrdersAppraiseService;
import com.zhuiYun.project.util.UploadFileUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;

import java.util.Date;

/**
 * LC: 2023-04-05 14:25
 */
@Service
public class OrdersAppraiseServiceImpl implements OrdersAppraiseService {

    @Autowired
    OrdersAppraiseMapper ordersAppraiseMapper;

    @Autowired
    UploadFileUtils uploadFile;

    /**
     * @descriptions 新增订单评价
     * LC: 2023-04-05 15:27
     */
    @Override
    public Integer saveOrdersAppraise(MultipartRequest file, MultipartFile[] multipartFiles, OrdersAppraiseEntity ordersAppraiseEntity) {
        if(multipartFiles!=null){
            String[] imgId = new String[multipartFiles.length];
            for (int ids=0;ids <multipartFiles.length;ids++) {
                String url = uploadFile.uploadFile(multipartFiles[ids]);
                if (!StringUtils.isEmpty(url)){
                    imgId[ids] = url;
                }else{
                    return 0;
                }
            }
            ordersAppraiseEntity.setOrdersFile(StringUtils.join(imgId,','));
        }
        //上传图片
        MultipartFile files = file.getFile("file");
        String s = uploadFile.uploadFile(files);
        ordersAppraiseEntity.setOrdersVideo(Integer.valueOf(s));
        ordersAppraiseEntity.setCreateTime(new Date());
        ordersAppraiseEntity.setDelFlag(0);
        return ordersAppraiseMapper.insertSelective(ordersAppraiseEntity);
    }
}
